1915F - Greetings - CodeForces Solution


data structures data structures data structures

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define lol 1000000007
#define fol 1000000000
#define all(c) c.begin(), c.end()
#define vi vector<int>
#define vvi vector<vi>
#define fi first
#define se second

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

int gcd(int a, int b)
{
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int expo(int n, int k, int mod)
{
  if (k == 0)
    return 1 % mod;
  int u = expo(n, k / 2, mod);
  u = (u * u) % mod;
  if (k % 2 != 0)
    u = (u * n) % mod;
  return u;
}

int sqrt(int n)
{
  if (n < 0)
    return -1;
  int L = 0, R = 2000000000;
  while (L < R)
  {
    int mid = (L + R) / 2;
    if (mid * mid >= n)
      R = mid;
    else
      L = mid + 1;
  }
  return L;
}

int factorial(int n)
{

  return (n == 1 || n == 0) ? 1 : int(n) * factorial(n - 1);
}

const int N = 50001;
vvi adj(N);
vi visited(N);

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  /*freopen("herding.in","r",stdin);
  freopen("herding.out","w",stdout); */
  int f = 1;
  cin >> f;
  while (f--)
  {
    int n;
    cin >> n;
    ordered_set s;
    vector<pair<int, int>> v(n);
    for (int i = 0; i < n; i++)
    {
      cin >> v[i].first;
      cin >> v[i].second;
      s.insert(v[i].second);
    }
    sort(all(v));
    long long ans = 0;
    for (int i = 0; i < n; i++)
    {
      ans += s.order_of_key(v[i].second);
     // ans -= s.order_of_key(v[i].first);
      s.erase(v[i].second);
    }
    cout << ans << "\n";
  }
}


Comments

Submit
0 Comments
More Questions

479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game